// pages/mine/integral/integral.js
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog"
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        integralList: [],
        isLoading: true,
        page_number: 1,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            integralList: [],
        })
        this.getIntegral()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            page_number: 1,
            integralList: [],
        })
        this.getIntegral()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.data.page_number += this.data.page_number
        this.getIntegral()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 获取积分列表
     * @param e
     */
    getIntegral: function (e) {
        const than = this
        http.get("integral/record/page", {
            area_code: app.globalData.areaCode,
            user_id: app.globalData.userInfo.applet_user_id,
            page_size: 10,
            page_number: than.data.page_number
        }).then(res => {
            if (res.data.code == 200) {
                res.data.data.list.forEach(item => {
                    than.data.integralList.push(item)
                })
                than.setData({
                    integralList: than.data.integralList,
                    isLoading: false
                })
                wx.stopPullDownRefresh();
                if (res.data.data.list.length == 0) {
                    Toast.fail("没有更多的数据了")
                } else {
                    Toast.success("数据加载完成")
                }
            } else {
                than.setData({
                    isLoading: false
                })
                Toast.fail(res.data.msg)
            }
        }, err => {
            than.setData({
                isLoading: false
            })
            Toast.fail("连接错误，请联系管理员！")
        })
    }
})